###############################################################################
# FL cleaning
#
# 10 August 2022
# sbaltz
###############################################################################
import numpy as np
import pandas as pd

RAW_FILE_NAME = "../transcribed/bay_scraped.csv"
SAVE_FILE_NAME = "../ready/bay_cleaned.csv"
COUNTY_NAME = "BAY"

ct = pd.read_csv(RAW_FILE_NAME)

cleaned = pd.DataFrame()

def RowIsFull(ct, #a dataframe
              i   #an integer representing a row number
              ):
    isFull = True
    colsToCheck = ["absO", "absA", "edO", "edA", "provO", "provA", "overO",\
                   "overA"]
    for c in colsToCheck:
        if str(ct[c][i]) == "nan":
            isFull = False
    return(isFull)

def GetModeVotes(mode, #plaintext of the voting mode
                 mode_to_vars, #maps mode to variable names
                 ct, #df
                 i #rownum
                 ):
    original = int(ct[mode_to_vars[mode]["original"]][i])
    audited = int(ct[mode_to_vars[mode]["audited"]][i])
    difference = audited - original
    return(original, audited, difference)

MODE_TO_VARS = {"ABSENTEE": {"original": "absO", "audited": "absA"},
                "IN-PERSON": {"original": "edO", "audited": "edA"},
                "PROVISIONAL": {"original": "provO", "audited": "provA"},
                "OVERSEAS": {"original": "overO", "audited": "overA"}
                }

office = ""
for i in range(len(ct)):
    if str(ct["candidate"][i]) == "Contest Audited:":
        office = str(ct["office"][i])
    if RowIsFull(ct, i):
        #Information that holds for all modes
        state = "FLORIDA"
        county = COUNTY_NAME
        method = "AUTOMATIC"
        candidate = ct["candidate"][i]
        
        #Absentee
        for mode in MODE_TO_VARS.keys():
            (original, audited, difference) = \
                    GetModeVotes(mode, MODE_TO_VARS, ct, i)
            cleaned = pd.concat([cleaned,
                        pd.DataFrame([[state,county,office,\
                        candidate,original,audited,difference,mode,method]])],\
                        ignore_index = True)


cleaned.columns = ["state","county","office","candidate","original","audited",\
                   "difference","mode","type"]

cleaned.to_csv(SAVE_FILE_NAME, index=False)
